package Java网络编程.TCP通信程序练习.p6_多线程实现文件上传;

import java.io.*;
import java.net.Socket;

public class ServerThread implements Runnable {
    private Socket s;

    public ServerThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            /*解决多文件命名冲突*/
            int count = 0;
            File file = new File("idea_test\\server[" + count + "].txt");
            while (file.exists()) {
                count++;
                file = new File("idea_test\\server[" + count + "].txt");
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));

            String line;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
                bw.flush();
            }

            //释放资源
            s.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
